package com.content_module.activity;

import android.annotation.SuppressLint;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.content_module.R;
import com.content_module.R2;
import com.content_module.adapter.MainAdapter;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import component.fy.com.common_module.base.BaseActivity;
import component.fy.com.common_module.constant.ARouterUtils;
import component.fy.com.common_module.baseAdapter.OnBaseAdapterItemClick;
import component.fy.com.common_module.utils.LogUtil;
import component.fy.com.common_module.utils.ToastUtil;
import component.fy.com.common_module.widget.SpaceItemDecoration;

@Route(path = ARouterUtils.ARouter_Content_Main)
public class MainAct extends BaseActivity {
    private final String TAG = this.getClass().getSimpleName();
    @BindView(R2.id.common_smart_refresh_layout_smart)
    SmartRefreshLayout smartRefreshLayout;
    @BindView(R2.id.common_smart_refresh_layout_recycler)
    RecyclerView recyclerView;

    private MainAdapter adapter;
    private List<String> list = new ArrayList<>();

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    LogUtil.e(TAG, "设置数据");
                    setData();
                    break;
            }
        }
    };

    @Override
    protected int getLayoutId() {
        return R.layout.content_act_main;
    }

    @Override
    protected void initView() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new SpaceItemDecoration(0, 0, 0, 2));
        recyclerView.setHasFixedSize(true);
        adapter = new MainAdapter(this, R.layout.content_item_main);
        recyclerView.setAdapter(adapter);
    }

    @Override
    protected void initEvents() {
        smartRefreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull final RefreshLayout refreshLayout) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.finishLoadMore();
                    }
                }, 1500);
            }

            @Override
            public void onRefresh(@NonNull final RefreshLayout refreshLayout) {
                handler.postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        refreshLayout.finishRefresh();
                    }
                }, 1500);
            }
        });
        adapter.setOnBaseAdapterItemClick(new OnBaseAdapterItemClick() {
            @Override
            public void onItemClick(View v, int position) {
                super.onItemClick(v, position);
                switch (position) {
                    case 0:
                        ARouterUtils.navigation(ARouterUtils.ARouter_App_Main);
                        finish();
                        break;
                    case 1:
                        goToPage(MainAct.this, BaseAdapterLayouShowAct.class);
                        break;
                    case 2:
                        goToPage(MainAct.this, TagFlowLayoutAct.class);
                        break;
                    case 3:
                        goToPage(MainAct.this, FlowTagViewAct.class);
                        break;
                    case 4:
                        goToPage(MainAct.this, BannerAndImagezoomAct.class);
                        break;
                    case 5:
                        goToPage(MainAct.this, ImagepickerAct.class);
                        break;
                    case 6:
                        goToPage(MainAct.this, SnapHelperAct.class);
                        break;
                    case 7:
                        goToPage(MainAct.this, AndroidPickerViewAct.class);
                        break;
                    case 8:
                        goToPage(MainAct.this, KotlinAct.class);
                        break;
                    case 9:
                        goToPage(MainAct.this, com.content_module.activity.workManager.MainAct.class);
                        break;
                    default:
                        ToastUtil.showToast("点击位置" + position);
                        break;
                }
            }
        });
    }

    @Override
    protected void initData() {
        handler.sendEmptyMessageDelayed(0, 2000);
    }

    private void setData() {
        list.add("0_返回主App");
        list.add("1_BaseRecyclerAdapter不同布局展示");
        list.add("2_TagFlowLayout");
        list.add("3_FlowTagView");
        list.add("4_Banner和查看大图功能");
        list.add("5_图片选择器");
        list.add("6_SnapHelper使用");
        list.add("7_三级联动滚轮控件");
        list.add("8_kotlin学习");
        list.add("9_WorkManager");
        adapter.setData(list);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        clearList(list);
    }
}
